package com.rusefi.maintenance.migration.afr_msq_import_migration;

import com.opensr5.ini.field.ArrayIniField;
import com.rusefi.config.FieldType;
import com.rusefi.maintenance.TestTuneMigrationContext;
import com.rusefi.maintenance.migration.migrators.ComposedTuneMigrator;
import com.rusefi.tune.xml.Constant;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import javax.xml.bind.JAXBException;
import java.util.Map;

import static com.rusefi.maintenance.migration.migrators.TableAddColumnsMigrator.LAMBDA_TABLE_FIELD_NAME;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class AfrLambdaTableAddColumnsMigrationTest {
    private TestTuneMigrationContext testContext;

    @BeforeEach
    void setUp() throws JAXBException {
        testContext = AfrMsqImportMigrationContext.loadSmallAfrToBigLambda();
        ComposedTuneMigrator.INSTANCE.migrateTune(testContext);

        final Map<String, Constant> migratedConstants = testContext.getMigratedConstants();
        assertEquals(1, migratedConstants.size());
        assertEquals("", testContext.getTestCallbacks().getContent());
    }

    @Test
    void checkLambdaTableMigration() {
        testContext.checkPrevAndUpdatedIniFields(
            LAMBDA_TABLE_FIELD_NAME,
            new ArrayIniField(
                LAMBDA_TABLE_FIELD_NAME,
                17256,
                FieldType.UINT8,
                16,
                16,
                "lambda",
                0.006802721088435374,
                "0.6",
                "1.5",
                "2"
            ),
            new ArrayIniField(
                LAMBDA_TABLE_FIELD_NAME,
                18848,
                FieldType.UINT8,
                24,
                16,
                "lambda",
                0.006802721088435374,
                "0.6",
                "1.5",
                "2"
            )
        );

        testContext.checkValueMigration(
            LAMBDA_TABLE_FIELD_NAME,
            new Constant(
                LAMBDA_TABLE_FIELD_NAME,
                "afr",
                "\n" +
                    "         14.7 14.7 14.7 14.8 14.9 15.0 15.1 15.2 15.2 15.2 15.2 15.2 15.2 15.2 15.2 15.2\n" +
                    "         14.7 14.7 14.7 14.8 14.9 15.0 15.1 15.2 15.2 15.2 15.2 15.2 15.2 15.2 15.2 15.2\n" +
                    "         14.7 14.7 14.7 14.8 14.9 15.0 15.1 15.2 15.2 15.2 15.2 15.2 15.2 15.2 15.2 15.2\n" +
                    "         14.7 14.7 14.7 14.8 14.9 15.0 15.1 15.2 15.2 15.2 15.2 15.2 15.2 15.2 15.2 15.2\n" +
                    "         14.7 14.7 14.7 14.8 14.9 15.0 15.1 15.2 15.2 15.2 15.2 15.2 15.2 15.2 15.2 15.2\n" +
                    "         14.7 14.7 14.7 14.8 14.9 15.0 15.1 15.2 15.2 15.2 15.2 15.2 15.2 15.2 15.2 15.2\n" +
                    "         14.7 14.7 14.7 14.8 14.9 15.0 15.1 15.2 15.2 15.2 15.2 15.2 15.2 15.2 15.2 15.2\n" +
                    "         14.2 14.2 14.2 14.2 14.3 14.4 14.5 14.5 14.5 14.5 14.5 14.5 14.5 14.5 14.5 14.5\n" +
                    "         13.9 13.9 13.9 13.9 14.0 14.1 14.1 14.2 14.2 14.1 14.1 14.1 14.1 14.1 14.1 14.1\n" +
                    "         13.6 13.6 13.6 13.7 13.7 13.8 13.8 13.8 13.8 13.8 13.8 13.8 13.8 13.7 13.7 13.7\n" +
                    "         13.1 13.1 13.1 13.1 13.1 13.1 13.1 13.1 13.1 13.1 13.1 13.1 13.0 13.0 13.0 13.0\n" +
                    "         12.8 12.8 12.8 12.8 12.8 12.8 12.8 12.8 12.8 12.7 12.7 12.7 12.7 12.6 12.6 12.6\n" +
                    "         12.8 12.8 12.8 12.8 12.8 12.8 12.8 12.8 12.8 12.7 12.7 12.7 12.7 12.6 12.6 12.6\n" +
                    "         12.8 12.8 12.8 12.8 12.8 12.8 12.8 12.8 12.8 12.7 12.7 12.7 12.7 12.6 12.6 12.6\n" +
                    "         12.8 12.8 12.8 12.8 12.8 12.8 12.8 12.8 12.8 12.7 12.7 12.7 12.7 12.6 12.6 12.6\n" +
                    "         12.8 12.8 12.8 12.8 12.8 12.8 12.8 12.8 12.8 12.7 12.7 12.7 12.7 12.6 12.6 12.6\n" +
                    "      ",
                "1",
                "16",
                "16"
            ),
            new Constant(
                LAMBDA_TABLE_FIELD_NAME,
                "lambda",
                "\n" +
                    "                   1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0\n" +
                    "                   1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0\n" +
                    "                   1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0\n" +
                    "                   1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0\n" +
                    "                   1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0\n" +
                    "                   0.952381 0.952381 0.952381 0.952381 0.952381 0.952381 0.952381 0.952381 0.952381 0.952381 0.952381 0.952381 0.952381 0.952381 0.952381 0.952381 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0\n" +
                    "                   0.9183673 0.9183673 0.9183673 0.9183673 0.9183673 0.9183673 0.9183673 0.9183673 0.9183673 0.9183673 0.9183673 0.9183673 0.9183673 0.9183673 0.9183673 0.9183673 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0\n" +
                    "                   0.8979592 0.8979592 0.8979592 0.8979592 0.8979592 0.8979592 0.8979592 0.8979592 0.8979592 0.8979592 0.8979592 0.8979592 0.8979592 0.8979592 0.8979592 0.8979592 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0\n" +
                    "                   0.8911565 0.8911565 0.8911565 0.8911565 0.8911565 0.8911565 0.8911565 0.8911565 0.8911565 0.8911565 0.8911565 0.8911565 0.8911565 0.8911565 0.8911565 0.8911565 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0\n" +
                    "                   0.877551 0.877551 0.877551 0.877551 0.877551 0.877551 0.877551 0.877551 0.877551 0.877551 0.877551 0.877551 0.877551 0.877551 0.877551 0.877551 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0\n" +
                    "                   0.8571429 0.8571429 0.8571429 0.8571429 0.8571429 0.8571429 0.8571429 0.8571429 0.8571429 0.8571429 0.8571429 0.8571429 0.8571429 0.8571429 0.8571429 0.8571429 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0\n" +
                    "                   0.8367347 0.8367347 0.8367347 0.8367347 0.8367347 0.8367347 0.8367347 0.8367347 0.8367347 0.8367347 0.8367347 0.8367347 0.8367347 0.8367347 0.8367347 0.8367347 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0\n" +
                    "                   0.8027211 0.8027211 0.8027211 0.8027211 0.8027211 0.8027211 0.8027211 0.8027211 0.8027211 0.8027211 0.8027211 0.8027211 0.8027211 0.8027211 0.8027211 0.8027211 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0\n" +
                    "                   0.7687075 0.7687075 0.7687075 0.7687075 0.7687075 0.7687075 0.7687075 0.7687075 0.7687075 0.7687075 0.7687075 0.7687075 0.7687075 0.7687075 0.7687075 0.7687075 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0\n" +
                    "                   0.7482993 0.7482993 0.7482993 0.7482993 0.7482993 0.7482993 0.7482993 0.7482993 0.7482993 0.7482993 0.7482993 0.7482993 0.7482993 0.7482993 0.7482993 0.7482993 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0\n" +
                    "                   0.7278912 0.7278912 0.7278912 0.7278912 0.7278912 0.7278912 0.7278912 0.7278912 0.7278912 0.7278912 0.7278912 0.7278912 0.7278912 0.7278912 0.7278912 0.7278912 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0\n" +
                    "            ",
                "2",
                "16",
                "24"
            ),
            new Constant(
                LAMBDA_TABLE_FIELD_NAME,
                "lambda",
                "\n" +
                    "         1.00 1.00 1.00 1.01 1.01 1.02 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03\n" +
                    "         1.00 1.00 1.00 1.01 1.01 1.02 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03\n" +
                    "         1.00 1.00 1.00 1.01 1.01 1.02 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03\n" +
                    "         1.00 1.00 1.00 1.01 1.01 1.02 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03\n" +
                    "         1.00 1.00 1.00 1.01 1.01 1.02 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03\n" +
                    "         1.00 1.00 1.00 1.01 1.01 1.02 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03\n" +
                    "         1.00 1.00 1.00 1.01 1.01 1.02 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03 1.03\n" +
                    "         0.97 0.97 0.97 0.97 0.97 0.98 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99 0.99\n" +
                    "         0.95 0.95 0.95 0.95 0.95 0.96 0.96 0.97 0.97 0.96 0.96 0.96 0.96 0.96 0.96 0.96 0.96 0.96 0.96 0.96 0.96 0.96 0.96 0.96\n" +
                    "         0.93 0.93 0.93 0.93 0.93 0.94 0.94 0.94 0.94 0.94 0.94 0.94 0.94 0.93 0.93 0.93 0.93 0.93 0.93 0.93 0.93 0.93 0.93 0.93\n" +
                    "         0.89 0.89 0.89 0.89 0.89 0.89 0.89 0.89 0.89 0.89 0.89 0.89 0.88 0.88 0.88 0.88 0.88 0.88 0.88 0.88 0.88 0.88 0.88 0.88\n" +
                    "         0.87 0.87 0.87 0.87 0.87 0.87 0.87 0.87 0.87 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86\n" +
                    "         0.87 0.87 0.87 0.87 0.87 0.87 0.87 0.87 0.87 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86\n" +
                    "         0.87 0.87 0.87 0.87 0.87 0.87 0.87 0.87 0.87 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86\n" +
                    "         0.87 0.87 0.87 0.87 0.87 0.87 0.87 0.87 0.87 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86\n" +
                    "         0.87 0.87 0.87 0.87 0.87 0.87 0.87 0.87 0.87 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86 0.86\n",
                "2",
                "16",
                "24"
            )
        );
    }
}
